$(document).ready(function() {
	  $('#punishMoney').attr("readonly","readonly");
      $('#punishDays').removeAttr("readonly");
	//处罚赋值
	var arr = add();
	for(var i=0; i<arr.length;i++){
	$("#punishName").append("<option value="+arr[i].punishDays+">"+arr[i].punishName+"</option>");
	}
	$("#punishDays").val($('#punishName option:selected').val());
	$("#punishName").change(function () {
		if($("#punishType").val()==1){
			$("#punishDays").val($('#punishName option:selected').val());
		}
	});

	punishchange();
	//回车模糊查询
	searchEvent();
	//下拉框查询
	changeEvent();
	//初始化数据
	init(1);
});
	
//默认加载  
function init(pageNo){
	//获取信息列表
	$("#thislist").html("");
	$.ajax({
		url: urlcore + "/api/account/jpa/page?pageNum="+pageNo+"&pageSize="+pageSize+"&search="+$('#search').val()+"&isRegister="+$('#change').val()+"&userType="+2,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success:function(data){
			if (data.success == true) {
				//i表示在data中的索引位置，n表示包含的信息的对象
				$.each(data.data.list,function(i,n){
					var id = n.user.id;
					if(n.user.realName==null){
						n.user.realName="";
					}
					var isRegister = isRegisterStr(n.user.isRegister);
					var serveType = serveTypeStr(n.user.serveType);
					var thislist = 
					'	<tr>'+
					'		<td>'+(i+1)+'</td>'+					
					'		<td>'+n.user.secondId+'</td>'+
					'		<td>'+n.user.realName+'</td>'+
					'		<td>'+n.user.phone+'</td>'+
					'		<td>'+n.allMoney+'</td>'+
					'		<td>'+n.depositMoney+'</td>';
					if(n.user.isRegister==3){
						thislist += '		<td style="color:red;">'+isRegister+'</td>';
					}else{
						thislist += '		<td>'+isRegister+'</td>';
					}
					if(n.user.serveType==2){
						thislist += '		<td style="color:red;">'+serveType+'</td>';
					}else{
						thislist += '		<td>'+serveType+'</td>';
					}
					thislist +=	
					'		<td>'+n.gmtDatetime+'</td>';
					if(n.user.isRegister!=0){
						thislist +=
						'		<td>'+					
						'			<a class="btn btn-primary btn-xs" href="rider_details.html?id='+id+'"> 详细</a>'+
				
						'           <a class="btn btn-primary btn-xs" href="javascript:;" data-toggle="modal" data-target="#punish" onclick="thisUpdate('+id+')"> 处罚或奖励</a>';
						
						if(n.user.isRegister == 2){
							thislist +=
								"&nbsp"+"&nbsp"+"&nbsp"+
								'<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="pass('+id+')"> 审核通过</a>'+
								"&nbsp"+"&nbsp"+"&nbsp"+
								'<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="fail('+id+')"> 审核失败</a>';
						}
					}

					thislist +=
					'		</td>'+
					'	</tr>';
					$('#thislist').append(thislist);										
				});
				$('#thiscount').text(data.data.total);
				$("#pager").pager({
					pagenumber: pageNo, 
					pagecount:data.data.pages,
					totalcount:data.data.total,
					buttonClickCallback: PageClick
				}); 											
			} else if (data.code == 'OVERTIME'){
				var thisUrl = window.location.href;
				if (thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href="login.html";
				}

			} else {
				if (data.msg != '空数据') {
					alert(data.msg)
				}else{
					$('#thiscount').text(0);
				}
			}

		},
		error:function() {
			alert("error");
		}
	});

	$.ajax({
        url: urlcore + "/api/cashRecord/userMoneySum?userType="+2+"&search="+$('#search').val()+"&isRegister="+$('#change').val(),
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success:function(data){
            if (data.success == true) {
            	var da=data.data;
            	if(JSON.stringify(da)=="{}"){           		
            		$('#allmoney').text(0);
	                $('#depositmoney').text(0);  
            	}else{
	                $('#allmoney').text(da.allmoney);
	                $('#depositmoney').text(da.depositmoney);          		
            	}
            } else if (data.code == 'OVERTIME'){
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href="login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error:function() {
            /* Act on the event */
            alert("error");
        }
    });
}

/**
 * 审核通过
 */
function pass(id){
    //拿到区域下的配送任务数据
   	var da='';
	$.ajax({
            url: urlcore + "/api/sendTask/sendTaskDetailsByUserId?userId="+id,
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
	                /* $('#messUrl').html(imgStr2);                  
                    $('#title').text(da.title);
                    $('#content').html(da.content);*/
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        var datwo="";
        $.ajax({
            url: urlcore + "/api/serveRate/serveRateByUserId?userId="+id,
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    datwo = data.data;                   
	                /* $('#messUrl').html(imgStr2);                  
                    $('#title').text(da.title);
                    $('#content').html(da.content);*/
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        
		$.ajax({
	        url: urlcore + "/api/user/setPass?userId="+id+"&worktimeMon="+da.worktimeMon+"&worktimeEve="+da.worktimeEve+"&changeDays="+da.changeDays+"&restDays="+da.restDays+"&dailyTasks="+da.dailyTasks+"&orderTicket="+da.orderTicket+"&fulltimeRate="+datwo.fulltimeRate,
	        type: "get",
	        dataType: 'json',
	        async: false,
	        contentType: "application/json;charset=utf-8",
	        success:function(data){
	            if (data.success == true) {
	            	location.reload();
	            } else if (data.code == 'OVERTIME'){
	                    var thisUrl = window.location.href;
	                    if (thisUrl.indexOf('login.html') <= -1) {
	                        top.window.location.href="login.html";
	                    }
	                } else {
	                    alert(data.msg);
	                }
	        },
	        error:function() {
	            /* Act on the event */
	            alert("error");
	        }
	    });
	    

}


/**
 * 审核失败
 */
function fail(id){
	$.ajax({
        url: urlcore + "/api/user/setFail?userId="+id,
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success:function(data){
            if (data.success == true) {
            	location.reload();
            } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
        },
        error:function() {
            /* Act on the event */
            alert("error");
        }
    });
}

function thisUpdate(id){	
	$("#thisId_update").val(id);
}

/**
 * 处罚
 */
function punish(){
	var id=$("#thisId_update").val();
	var punishType=$("#punishType").val();
	var punishMoney=$("#punishMoney").val();
	var punishName=$("#punishName option:checked").text();
	var punishDays=$("#punishDays").val();
	$.ajax({
        url: urlcore + "/api/punishLog/punishUser?id="+id+"&punishType="+punishType+"&punishMoney="+punishMoney+"&punishName="+punishName+"&punishDays="+punishDays,
        type: "get",
        dataType: 'json',
        contentType: "application/json;charset=utf-8",
        success:function(data){
            if (data.success == true) {
            	location.reload();
            } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
        },
        error:function() {
            /* Act on the event */
            alert("error");
        }
    });
}

//给处罚弹窗赋值
function add(){
	var da='';
	var punishType=$("#punishType").val();
	$.ajax({
            url: urlcore + "/api/punishSetting/allPunish?userType="+1+"&punishType="+punishType,
            type: "get",
            dataType: 'json',
            contentType: "application/json;charset=utf-8",
            async: false,
            success:function(data){
                if (data.success == true) {
                    da = data.data;                   
                } else if (data.code == 'OVERTIME'){
                    var thisUrl = window.location.href;
                    if (thisUrl.indexOf('login.html') <= -1) {
                        top.window.location.href="login.html";
                    }
                } else {
                    alert(data.msg);
                }
            },
            error:function() {
                /* Act on the event */
                alert("error");
            }
        });
        return da;
}

function punishchange(){
		var arr = new Array();
	    $("#punishType").change(function () {
       		if($("#punishType").val()==1){
       			$('#punishMoney').attr("readonly","readonly");
       			$('#punishDays').removeAttr("readonly");
       			
       			$("#punishName").empty();
       			$("#punishMoney").val("");
		     	arr = add();
				for(var i=0; i<arr.length;i++){
					$("#punishName").append("<option value="+arr[i].punishDays+">"+arr[i].punishName+"</option>");
				}
				$("#punishDays").val($('#punishName option:selected').val());
				$("#punishName").change(function () {
					if($("#punishType").val()==1){
						$("#punishDays").val($('#punishName option:selected').val());
					}
   				});
       		}
       		if($("#punishType").val()==2||$("#punishType").val()==3){
       			$('#punishDays').attr("readonly","readonly");
       			$('#punishMoney').removeAttr("readonly");
       			
       			$("#punishName").empty();
       			$("#punishDays").val("");
	       		arr = add();
				for(var i=0; i<arr.length;i++){
					$("#punishName").append("<option value="+arr[i].punishMoney+">"+arr[i].punishName+"</option>");
				}
				$("#punishMoney").val($('#punishName option:selected').val());
				$("#punishName").change(function () {
					if($("#punishType").val()==2||$("#punishType").val()==3){
						$("#punishMoney").val($('#punishName option:selected').val());	
					}			    	
       		   	});
       		}  
       	});	
}

//回调函数  
PageClick = function(pageclickednumber) {  
    init(pageclickednumber); 
}

/**
 * 模糊查询
 */
function searchEvent(){
	$('#search').bind('keypress',function(event){                 
         if(event.keyCode == 13) {  
              init(1);
         }  
	});
}

/**
 * 下拉框查询
 */
function changeEvent(){
       $("#change").change(function () {
       		init(1);
   		 });
}


/**
 * 用户状态返回
 * @param {Object} isRegister
 */
function isRegisterStr(isRegister){
	if(isRegister==1) {
		isRegister = '已通过';
	}
	else if(isRegister==2) {
		isRegister = '待审核';
	}	else if(isRegister==3) {
		isRegister = '未通过';
	}else if(isRegister==0) {
		isRegister = '未认证';
	}
	return isRegister;
}

/**
 * 用户状态返回
 * @param {Object} serveType
 */
function serveTypeStr(serveType){
	if(serveType==1) {
		serveType = '正常';
	}
	else if(serveType==2) {
		serveType = '处罚';
	}
	return serveType;
}

